﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using DataTransfer;
namespace BUS
{
    public class QuiDinhBus
    {
        public static string connect = ConfigurationManager.ConnectionStrings["DataTransfer.Properties.Settings.QLPMConnectionString"].ConnectionString;
        public static QLPMDataContext db;
       // db = new QLPMDataContext(connect);
        public IEnumerable<QuiDinh> GetAll()
        {
            IEnumerable<QuiDinh> qd = null;
            try
            {
                db = new QLPMDataContext(connect);
                qd = from t in db.QuiDinhs select t;
                //db.Dispose();

            }
            catch (Exception)
            {

                throw;
            }
            return qd;

        
        }
        public int Insert(QuiDinh info)
        {
            int id = -1;
            try
            {
                db = new QLPMDataContext(connect);
                db.QuiDinhs.InsertOnSubmit(info);
                db.SubmitChanges();
                
                id = info.ID;
            }
            catch (Exception)
            {

                throw;
            }

            return id;
        }
        public QuiDinh Get(int key)
        {
            QuiDinh info = new QuiDinh();
            info = null;
            try
            {
                db = new QLPMDataContext(connect);
                info = (from qd in db.QuiDinhs where qd.ID == key select qd).SingleOrDefault();
            }
            catch (Exception)
            {

                throw;
            }
            return info;
        }


        public bool Delele(int key)
        {
            bool flag = false;
            try
            {
               
               db = new QLPMDataContext(connect);
               
               QuiDinh del = (from t in db.QuiDinhs where t.ID == key select t).SingleOrDefault();
               
                db.QuiDinhs.DeleteOnSubmit(del);
                
                db.SubmitChanges();
               
                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
        public bool Update(QuiDinh info)
        {
            bool flag = false;
            try
            {
                db = new QLPMDataContext(connect);
                var t = (from qd in db.QuiDinhs where qd.ID == info.ID select qd).SingleOrDefault();
                t.QD = info.QD;
                t.Value = info.Value;
                t.UserModify = info.UserModify;
                t.ModifyDate = info.ModifyDate;
                db.SubmitChanges();
                
               
                flag = true;

            }
            catch (Exception)
            {
                
                throw;
            }
            return flag;
        }
    }
}
